﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

public partial class View_CashManager_CMAEHasReportOfBank : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void RadGrid_ItemUpdated(object source, GridCommandEventArgs e)
    {
        //Get Editing Item Id
        int idEditing = int.Parse(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["Id"].ToString());

        //Get Edited Item
        GridEditableItem editedItem = e.Item as GridEditableItem;

        Hashtable hastTable = new Hashtable();
        //Add an Editing Item Id into hash 
        hastTable.Add("Id", idEditing);
        //The GridTableView will fill the values from all editable columns in the hash
        e.Item.OwnerTableView.ExtractValuesFromItem(hastTable, editedItem);

        //Get Datatable in Data Grid
        DataTable dt = (DataTable)Session["CMAEHasReportOfBank"];

        //Edit Item of Datatable
        foreach (DataRow row in dt.Rows)
        {
            if (int.Parse(row["Id"].ToString()) == idEditing)
            {
                foreach (DictionaryEntry de in hastTable)
                {
                    row[de.Key.ToString()] = de.Value;
                }
            }
        }

        //Cancel Edit Mode
        grCMHRAEInfo.EditIndexes.Clear();

        //Update Datatable Session
        Session["CMAEHasReportOfBank"] = dt;
    }

    protected void RadGrid_ItemInserted(object source, GridCommandEventArgs e)
    {
        //Get Edited Item
        GridEditableItem editedItem = e.Item as GridEditableItem;

        //Get Datatable in Data Grid
        DataTable dt = (DataTable)Session["CMAEHasReportOfBank"];

        DataRow newRow = dt.NewRow();

        //Get All Row in Datatable
        DataRow[] allValues = dt.Select("", "Id", DataViewRowState.CurrentRows);

        //Assign new Row Id 
        if (allValues.Length > 0)
        {
            newRow["Id"] = (int)allValues[allValues.Length - 1]["Id"] + 1;
        }
        else
        {
            newRow["Id"] = 1; //The table is empty
        }

        Hashtable newValues = new Hashtable();
        //The GridTableView will fill the values from all editable columns in the hash
        e.Item.OwnerTableView.ExtractValuesFromItem(newValues, editedItem);

        try
        {
            foreach (DictionaryEntry de in newValues)
            {
                newRow[(string)de.Key] = de.Value;
            }
        }
        catch (Exception ex)
        {
            throw (ex);
            e.Canceled = true;
        }

        //Insert New Row Into Datatable
        dt.Rows.Add(newRow);

        //Cancel Insert Mode
        grCMHRAEInfo.MasterTableView.ClearEditItems();

        //Update Mode
        Session["Mode"] = "Inserted";

        //Update Datatable Session
        Session["CMAEHasReportOfBank"] = dt;
    }

    protected void RadGrid_ItemDeleted(object source, GridDeletedEventArgs e)
    {

    }

    protected void RadGrid_Init(object sender, System.EventArgs e)
    {
        GridFilterMenu menu = grCMHRAEInfo.FilterMenu;
        int i = 0;
        while (i < menu.Items.Count)
        {
            if (menu.Items[i].Text == "NoFilter")
            {
                menu.Items[i].Text = "Không lọc";
                i++;
            }
            else if (menu.Items[i].Text == "EqualTo")
            {
                menu.Items[i].Text = "Bằng";
                i++;
            }
            else if (menu.Items[i].Text == "GreaterThan")
            {
                menu.Items[i].Text = "Lớn hơn";
                i++;
            }
            else if (menu.Items[i].Text == "LessThan")
            {
                menu.Items[i].Text = "Nhỏ hơn";
                i++;
            }
            else if (menu.Items[i].Text == "GreaterThanOrEqualTo")
            {
                menu.Items[i].Text = "Lớn hơn hoặc bằng";
                i++;
            }
            else if (menu.Items[i].Text == "LessThanOrEqualTo")
            {
                menu.Items[i].Text = "Nhỏ hơn hoặc bằng";
                i++;
            }
            else
            {
                menu.Items.RemoveAt(i);
            }
        }
    }

    protected void RadGrid_PreRender(object sender, EventArgs e)
    {
        HideExpandColumnRecursive(grCMHRAEInfo.MasterTableView);
    }

    public void HideExpandColumnRecursive(GridTableView tableView)
    {
        GridItem[] nestedViewItems = tableView.GetItems(GridItemType.NestedView);
        foreach (GridNestedViewItem nestedViewItem in nestedViewItems)
        {
            foreach (GridTableView nestedView in nestedViewItem.NestedTableViews)
            {
                if (nestedView.Items.Count == 0)
                {
                    TableCell cell = nestedView.ParentItem["ExpandColumn"];
                    cell.Controls[0].Visible = false;
                    nestedViewItem.Visible = false;
                }
                if (nestedView.HasDetailTables)
                {
                    HideExpandColumnRecursive(nestedView);
                }
            }
        }
    }

    protected void RadGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        DataTable table = (DataTable)Session["CMAEHasReportOfBank"];
        if (Session["Mode"] == null)
        {
            _generalVoucherBAL = new GAAEGeneralVoucherBAL();
            Session["CMAEHasReportOfBank"] = _generalVoucherBAL.emptyTable();
            grCMHRAEInfo.DataSource = Session["CMAEHasReportOfBank"];
        }
        else if ((string)Session["Mode"] == "Inserted")
        {
            grCMHRAEInfo.DataSource = table;
        }
    }
}